package com.zuxia.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zuxia.dao.IRecruitDao;
import com.zuxia.entity.Posit;
import com.zuxia.entity.Recruit;
@Component
public class RecruitDao implements IRecruitDao{
	@Autowired
	private SessionFactory sf;
	@Override
	public List<Recruit> selectRecruit(int pageNow, String yname, int pageSize) {
		// TODO Auto-generated method stub
		Session session = sf.getCurrentSession();
		Criteria criteria = null;
		if(yname != null || yname != ""){
			criteria = session.createCriteria(Recruit.class).add(Restrictions.like("recruitMent", "%"+yname+"%")).addOrder(Order.desc("recruitId"));
		}else{
			criteria = session.createCriteria(Recruit.class).addOrder(Order.desc("recruitId"));
		}
		criteria.setFirstResult((pageNow - 1) * pageSize);
		criteria.setMaxResults(pageSize);
		List list = criteria.list();
		return list;
	}

	@Override
	public int recsize() {
		// TODO Auto-generated method stub
		Session session = sf.getCurrentSession();
		return session.createCriteria(Recruit.class).list().size();
	}

	@Override
	public void delrec(Recruit recruit) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();
		Transaction tran = session.beginTransaction();
		session.delete(recruit);
		tran.commit();
		session.close();
	}

	@Override
	public void addrec(Recruit recruit) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();
		Transaction tran = session.beginTransaction();
		session.save(recruit);
		tran.commit();
		session.close();
	}

	@Override
	public Recruit findupdateRecruit(Recruit recruit) {
		// TODO Auto-generated method stub
		Session session = sf.getCurrentSession();
		return session.get(Recruit.class, recruit.getRecruitId());
		 
	}

	@Override
	public void updrec(Recruit recruit) {
		// TODO Auto-generated method stub
		Session session = sf.openSession();
		Transaction tran = session.beginTransaction();
		session.update(recruit);	
		tran.commit();
		session.close();
	}

}
